/*
 * StdNowPlayingModule.cpp
 *
 *  Created on: 27.07.2009
 *      Author: h3llfire
 */

#include "StdNowPlayingModule.h"
#include "../StdVariableModule/StdVariableModule.h"
#include "../../core/Impp.h"

namespace Impp {

IMPLEMENT_DYNAMIC_CLASS(StdNowPlayingModule, iModule)

StdNowPlayingModule::StdNowPlayingModule() {

}

StdNowPlayingModule::~StdNowPlayingModule() {

}

void StdNowPlayingModule::moduleInit(){
	timer = new wxTimer(this, wxID_ANY);
	Connect(wxEVT_TIMER, wxTimerEventHandler(StdNowPlayingModule::onTimer), NULL, this);


	Impp* impp = Impp::exemplar();
	module = impp->getDatabase()->getModule(wxT("StdNowPlayingModule"));

	setEnabled((bool)module.getIntValue(wxT("enabled"), 0));

	SettingsPage* page = impp->getSettings()->createSettingsPage(module, wxT("Now Playing Info"));
	SettingsGroup* global = page->createSettingsGroup(wxT("Global"));
	global->createCheckSetting(wxT("Enabled"), wxT("enabled"));

	impp->Connect(EVT_SETTING_CHANGED, SettingChangedEventHandler(StdNowPlayingModule::onSettingChanged), NULL, this);
	impp->Connect(EVT_OWN_STATUS_CHANGING, StatusEventHandler(StdNowPlayingModule::onOwnStatusChanging), NULL, this);
}

void StdNowPlayingModule::moduleDestroy(){
	delete timer;
}

void StdNowPlayingModule::setEnabled(bool enabled){
	module.setIntValue(wxT("enabled"),enabled);
	if(enabled){
		timer->Start(10000, false);
	}else{
		timer->Stop();
	}
}


void StdNowPlayingModule::onOwnStatusChanging(StatusEvent & event){

	StdVariableModule* varModule = dynamic_cast<StdVariableModule*>(Impp::exemplar()->getModuleManager()->getModuleByClass(STD_VARIABLE_MODULE_NAME));

	if(varModule != NULL){
		wxString result = varModule->parseString(event.getStatusMsg());
		event.changeStatusMsgTo(result);
	}else{
		wxLogDebug(wxT("No VariableModule found!"));
	}

	event.Skip();
}

void StdNowPlayingModule::onSettingChanged(SettingChangedEvent & event){
	if(event.getModule() == module){
		if(event.getSettingName() == wxT("enabled")){
			setEnabled(event.IsChecked());
			wxLogDebug(wxT("enabled: %i"), event.IsChecked());
		}
	}else{
		event.Skip();
	}
}

void StdNowPlayingModule::onTimer(wxTimerEvent & event){
	TrackInfo newInfo;
	Banshee::getInfo(&newInfo);

	if(newInfo.status != PLAYER_CLOSED){

		if(curTrackInfo.artist != newInfo.artist || curTrackInfo.track != newInfo.track){
			wxLogDebug(wxT("new Song: %s - %s"), newInfo.artist.c_str(), newInfo.track.c_str());

			Impp* impp = Impp::exemplar();
			std::vector<Account*> accounts;
			impp->getActiveAccounts(accounts);

			StdVariableModule* varModule = dynamic_cast<StdVariableModule*>(Impp::exemplar()->getModuleManager()->getModuleByClass(STD_VARIABLE_MODULE_NAME));

			if(varModule != NULL){
				varModule->setVar(wxT("artist"), newInfo.artist);
				varModule->setVar(wxT("track"), newInfo.track);
				varModule->setVar(wxT("album"), newInfo.album);
			}else{
				wxLogDebug(wxT("No VariableModule found!"));
			}

			for(size_t i = 0; i < accounts.size(); i++){
				Account* acc = accounts[i];
				if(acc->protocol->getConnectionState() == CONNECTION_STATE_CONNECTED){
					impp->setStatus(acc->id, acc->self->getStatus(), acc->self->getRawStatusMsg());
				}
			}
			curTrackInfo = newInfo;
		}
	}

}

}
